SOURCE_DIR=/source
NUPKG_DIR=/nupkg
# export NUGET_HTTP_CACHE_PATH=/nuget/v3-cache
export NUGET_SCRATCH=/nuget/plugins-cache
export NUGET_PLUGINS_CACHE_PATH=/nuget/NuGetScratch
export NUGET_PACKAGES=/nuget/packages


function exec(){
    # 统计执行时间
    start_time=$(date +%s%N)
    echo "******************************************************************"
    echo "> $1"
    eval $1
    error=$?
    end_time=$(date +%s%N)
    runtime=$(( (end_time - start_time) / 1000000 ))
    echo "耗时: $runtime ms"
    if [ $error -ne 0 ]; then
        echo "异常中断: $error"
        exit $error
    fi
    echo "******************************************************************"
}


echo "=================================================================="
echo "==== file version 2025-08-18 ====================================="
echo "=================================================================="
echo "============ dir ================================================="
echo "SOURCE_DIR                = $SOURCE_DIR"
echo "NUPKG_DIR                 = $NUPKG_DIR"
echo "NUGET_PACKAGES            = $NUGET_PACKAGES"
echo "NUGET_HTTP_CACHE_PATH     = $NUGET_HTTP_CACHE_PATH"
echo "NUGET_SCRATCH             = $NUGET_SCRATCH"
echo "NUGET_PLUGINS_CACHE_PATH  = $NUGET_PLUGINS_CACHE_PATH"
echo "=================================================================="

exec "mkdir -pv $NUPKG_DIR"

if [ "$MODE" == "" ]; then
    MODE="Release"
fi

if [ "$TARGET" == "" ]; then
    TARGET=$SOURCE_DIR
else
    TARGET=$(realpath -m $TARGET)
fi

if [ "$TIMEOUT" == "" ]; then
    TIMEOUT=30
fi

if [ "$VERSION_SUFFIX" == "*" ]; then
    VERSION_SUFFIX=$(($(date "+%s")-$(date -d '2020-01-01' "+%s")))
elif [ ! "$VERSION_SUFFIX" == "" ]; then
    VERSION_SUFFIX=$VERSION_SUFFIX
fi

if [ "$LOG_LEVEL" == "" ]; then
    LOG_LEVEL=m
fi

echo "============= env ================================================"
echo "GIT            = $GIT"
echo "TARGET         = $TARGET"
echo "PUSH_ONLY      = $PUSH_ONLY"
echo "MODE           = $MODE"
echo "NUGET_SOURCE   = $NUGET_SOURCE"
echo "NUGET_PUSH_URL = $NUGET_PUSH_URL"
echo "NUGET_PUSH_KEY = $NUGET_PUSH_KEY"
echo "VERSION_SUFFIX = $VERSION_SUFFIX"
echo "LOG_LEVEL      = $LOG_LEVEL"
echo "TIMEOUT        = $TIMEOUT (s)"
echo "=================================================================="


if [ ! "$PUSH_ONLY" == "true" ]; then

    if [ ! "$GIT" == "" ]; then
        exec "git clone --depth 1 $GIT /source"
    fi

    dotnet_restore="dotnet restore \"$TARGET\" -v $LOG_LEVEL --ignore-failed-sources -property:WarningLevel=0"
    if [ ! "$NUGET_SOURCE" == "" ]; then
        for s in $(echo $NUGET_SOURCE | tr ";" "\n"); do
            dotnet_restore="$dotnet_restore -s \"$s\""
        done
        dotnet_restore="$dotnet_restore -s https://api.nuget.org/v3/index.json"
    fi
    exec "$dotnet_restore"

    for nupkg in $(find $NUPKG_DIR -name "*.nupkg"); do
        exec "rm -f $nupkg"
    done

    if [ ! "$VERSION_SUFFIX" == "" ]; then
        VERSION_SUFFIX=" --version-suffix \"$VERSION_SUFFIX\""
    fi

    dotnet_build="dotnet build \"$TARGET\" --no-restore --nologo -property:WarningLevel=0 -c $MODE -v $LOG_LEVEL --no-incremental --force"

    exec "$dotnet_build $VERSION_SUFFIX"

    dotnet_pack="dotnet pack \"$TARGET\" --no-restore --no-build --nologo -p:OutputType= -property:WarningLevel=0 -c $MODE -v $LOG_LEVEL --output \"$NUPKG_DIR\""

    exec "$dotnet_pack $VERSION_SUFFIX"

fi

echo "正在获取待推送nupkg包:"
ls $NUPKG_DIR/*.nupkg

for nupkg in $(find $NUPKG_DIR -name "*.nupkg"); do
    exec "dotnet nuget push \"$nupkg\" -s $NUGET_PUSH_URL -k $NUGET_PUSH_KEY --no-symbols --disable-buffering --skip-duplicate --no-service-endpoint -t $TIMEOUT" | while read i 
    do
        if [[ "$i" == "Usage: dotnet nuget push"* ]]; then
            break
        else
            echo $i
        fi
    done
done
echo "************ end *************************************************"